package com.wfx.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jwt.util.JwtUtils;
import com.jwt.util.RsaUtils;
import com.wfx.entity.Result;
import com.wfx.entity.WxbCustomer;
import com.wfx.entity.WxbMemeber;
import jdk.nashorn.internal.ir.CallNode;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import org.springframework.util.ResourceUtils;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.PublicKey;
import java.util.Map;

public class MyBasicAuthenticationFilter extends BasicAuthenticationFilter {


    public MyBasicAuthenticationFilter(AuthenticationManager authenticationManager) {
        super(authenticationManager);
    }


    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        //获取token
        String token = request.getHeader("token");
        if (token == null) {
            myResponse(response,new Result(false,"token为空"));
            return;
        }
        //使用公钥校验jwt令牌
        try {
            String publicKeyPath = ResourceUtils.getFile("classpath:ras_public").getPath();
            PublicKey publicKey = null;
            publicKey = RsaUtils.getPublicKey(publicKeyPath);
            //解析数据
            WxbMemeber wxbMemeber= (WxbMemeber) JwtUtils.getInfoFromToken(token, publicKey, WxbMemeber.class);
            //放入上下文环境
            UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(wxbMemeber,null,null);
            SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
            //放行
            chain.doFilter(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            myResponse(response,new Result(false,"token有误"));
        }
    }



    private void myResponse(HttpServletResponse response,Result result){
        try {
            response.setContentType("application/json;charset=utf-8");
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            PrintWriter writer = response.getWriter();
            writer.write(new ObjectMapper().writeValueAsString(result));
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }





    private void response(HttpServletResponse response, Result res) {
        try {
            response.setContentType("application/json;charset=utf-8");
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            PrintWriter out = response.getWriter();

            out.write(new ObjectMapper().writeValueAsString(res));
            out.flush();
            out.close();
        }catch (Exception e){

        }

    }

}
